/*
 * enc_json_util.h
 *
 *  Created on: 2018年11月3日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#ifndef ENC_JSON_UTIL_H_
#define ENC_JSON_UTIL_H_


#ifndef GET_BOOL_VALUE
#define GET_BOOL_VALUE(p, key, v, default_val) do {	\
	int on = default_val;	\
	if (v.HasMember(key)) {	\
		if (v[key].IsString()) {	\
			std::string v_tmp = v[key].GetString();	\
			std::transform(v_tmp.begin(), v_tmp.end(), v_tmp.begin(), ::tolower);	\
			if (v_tmp == "true") {	\
				on = true;	\
			} else if (v_tmp == "false") {	\
				on = false;	\
			} else {	\
				on = atoi(v[key].GetString());	\
			}	\
		} else if (v[key].IsInt())	{ \
			on = v[key].GetInt();	\
		} else if (v[key].IsBool()) {	\
			on = v[key].GetBool();	\
		}	\
		if (on == 0 || on == 1) {	\
			p = !!on;	\
		}	\
	} else {	\
		p = !!on;	\
	}	\
}while(0);
#endif

#ifndef GET_INT_VALUE
#define GET_INT_VALUE(p, key, v, default_val) do {	\
	int v_tmp = default_val;	\
	if (v.HasMember(key)) {	\
		if (v[key].IsString()) {	\
			v_tmp = atoi(v[key].GetString());	\
		} else if (v[key].IsInt())	{ \
			v_tmp = v[key].GetInt();	\
		}	\
		p = v_tmp;	\
	} else {	\
		p = v_tmp; \
	}	\
}while(0);
#endif

#ifndef GET_INT64_VALUE
#define GET_INT64_VALUE(p, key, v, default_val) do {	\
	int v_tmp = default_val;	\
	if (v.HasMember(key)) {	\
		if (v[key].IsString()) {	\
			v_tmp = atoi(v[key].GetString());	\
		} else if (v[key].IsInt64())	{ \
			v_tmp = v[key].GetInt64();	\
		}	\
		p = v_tmp;	\
	} else {	\
		p = v_tmp; \
	}	\
}while(0);
#endif

#ifndef GET_HEX_VALUE
#define GET_HEX_VALUE(p, key, v, default_val) do {	\
	uint val = default_val;	\
	if (v.HasMember(key)) {	\
		if (v[key].IsString()) {	\
			val = enc_hex2int(v[key].GetString());	\
		} else if (v[key].IsInt())	{ \
			val = v[key].GetInt();	\
		}	\
		p = val;	\
	} else {	\
		p = val; \
	}	\
}while(0);
#endif

#ifndef GET_DOUBLE_VALUE
#define GET_DOUBLE_VALUE(p, key, v, default_val) do {	\
	double v_tmp = default_val;	\
	if (v.HasMember(key)) {	\
		if (v[key].IsString()) {	\
			v_tmp = atof(v[key].GetString());	\
		} else if (v[key].IsDouble())	{ \
			v_tmp = v[key].GetDouble();	\
		} else if (v[key].IsInt())	{ \
			v_tmp = v[key].GetInt();	\
		}		\
		p = v_tmp;	\
	} else {	\
		p = v_tmp; \
	}	\
}while(0);
#endif

#ifndef GET_STR_VALUE
#define GET_STR_VALUE(p, key, v, default_val) do { \
	p.clear();	\
	p = default_val;	\
	if (v.HasMember(key) && v[key].IsString()) {	\
		p = v[key].GetString();	\
		u::util::Str::trim(p);	\
	} 	\
}while(0);
#endif

#endif /* ENC_JSON_UTIL_H_ */
